/**
 * Created by kevin on 2017/01/08.
 *
 * 通用UI层，全局导航按钮
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application
    function UILayer() {
        this.Container_constructor();
        this.init();
    }


    var p = createjs.extend(UILayer, createjs.Container);
    p.className = "UILayer";
    //加载器
    p.loader;
    //图标
    p.UIConfigs = [];
    p.menuGroupIndex = -1;

    // public methods:
    p.init = function () {
        this.loadRes();
    }

    /*
     * 加载资源
     * */
    p.loadRes = function(){
        this.loader = new createjs.LoadQueue(true);

        this.loader.on("fileload", this.onLoaded, this);
        /*if(this.manifest != undefined){
            this.loader.loadManifest(this.manifest, true, "./assets/");
        }else{
            this.loader.loadManifest({src: this.resObj.path + "?v="+version, callback: this.resObj.callback, type: "manifest"}, true, "./assets/");
        }*/
        this.loader.loadManifest({src: BASE_PATH + "static/UILayerManifest.json?v="+version, callback: "loadUIMediaGrid", type: "jsonp"}, true, "");

    }
    /*
    * 资源加载
    * */
    p.onLoaded = function (evt){
        //console.log(evt.loader.type)
        if(evt.loader.type == "jsonp"){
            var result = evt.result;

            this.UIConfigs=result;
            //console.log(this.pages)
            //console.log(cur_page);
            for(var i = 0;i<this.UIConfigs.length;i++){
                //console.log(this.UIConfigs[i])
                switch (this.UIConfigs[i].type){
                    case "class":
                        this.drawClassType(this.UIConfigs[i]);
                        break;
                    case "group_menu":
                        this.menuGroupIndex = i;
                        this.loader.loadManifest(this.UIConfigs[i].icons);
                        this.loader.on("complete", this.loadComplete, this);
                        //this.drawGroupMenu(this.UIConfigs[i]);
                        break;
                }
            }
        }


    }
    /*
     * 资源加载完毕
     * */
    p.loadComplete = function (evt){
        //console.log(evt)
        this.drawGroupMenu(this.UIConfigs[this.menuGroupIndex]);
    }
    /*
    * 按钮组
    * */
    p.drawGroupMenu = function(config){
        //console.log(config)
        //return;
        for(var i = 0 ;i<config.icons.length;i++){
            var pic = this.loader.getResult(config.icons[i].id);
            var item;
            switch(config.icons[i].r_type){
                case "bitmap":
                    item = new createjs.Bitmap(pic);
                    break;
                case "spritesheet":
                    var draw_content=config.icons.prop["draw"];//精灵绘制配置对象
                    item = new eosi.website.BaseSpriteSheet(pic,draw_content);
                    break;
                case "baseslide":
                    item = new eosi.website.BaseSlide(pic);
                    break;
                case "button":
                    var bitmap = new createjs.Bitmap(pic);
                    item = new eosi.website.BaseButton(bitmap);
                    //item.addChild(bitmap);
                    break;
                default :
                    item = new createjs.Bitmap(pic);
                    break;
            }

            item.name = config.icons[i].id;
            item.resIndex = i;
            item.config = config.icons[i];
            //console.log(item.name)
            this.setScale(item,config.icons[i]);
            this.setPos(item,config.icons[i]);
            //animation
            this.setAnimation(item,config.icons[i]);

            this.addChild(item);
            //action
            this.setAction(item,config.icons[i]);
        }
    }

    /*
     * 设置scale
     * */
    p.setScale = function(item,config){
        var pic = item;
        // this.loader.getResult(this.resItems[i].id);
        switch (config.prop.fit){
            case "fit_all"://强制全屏
                //item.setTransform(0,0,targetWidth/pic.getBounds().width,targetHeight/pic.getBounds().height);
                item.scaleX= targetWidth/pic.getBounds().width
                item.scaleY = targetHeight/pic.getBounds().height
                //console.log(item.scaleX)
                break;
            case "fit_screen"://根据屏幕比例适配，不变性(相对于屏幕的比例)
                item.regX = pic.getBounds().width*.5;
                item.regY = pic.getBounds().height*.5;
                var ratio = config.prop.scale*targetWidth/pic.getBounds().width;
                //item.setTransform(0,0,ratio,ratio);
                item.scaleX=item.scaleY= ratio;

                if(item.oriScale){
                    item.oriScale = ratio
                }
                break;
            default :
                break;
        }
    }

    /*
     * 设置位置
     * */
    p.setPos = function(item,config){
        if(config.pos.type == "abs"){//绝对位置
            switch (config.prop.align){
                case "TL":
                    item.x = 0;
                    item.y = 0;
                    break;
                case "TR":
                    item.x = targetWidth;
                    item.y = 0;
                    break;
                case "TM":
                    item.x = targetWidth*0.5;
                    item.y = 0;
                    break;
                case "BL":
                    item.x = 0;
                    item.y = targetHeight;
                    break;
                case "BR":
                    item.x = targetWidth;
                    item.y = targetHeight;
                    break;
                case "BM":
                    item.x = targetWidth*.5;
                    item.y = targetHeight;
                    break;
                case "MM":
                    item.x = targetWidth*.5;
                    item.y = targetHeight*.5;
                    break;
            }
        }else if(config.pos.type == "rel"){//相对位置
            var father = this.getChildByName(config.pos.father);
            //console.log(father)
            if(father){
                item.x = father.x;
                item.y = father.y;
                //如果父亲也有动画，使用目标值作为参数的zhi
                if(father.proObj!=undefined){
                    //console.log(father.proObj)
                    if(father.proObj.y !=undefined){
                        item.y = father.proObj.y;
                    }
                    if(father.proObj.x !=undefined){
                        item.y = father.proObj.x;
                    }

                }

            }

        }

        item.x+=config.prop.offset.x * targetWidth;
        item.y+=config.prop.offset.y * targetHeight;
    }

    /*
     * 设置action
     * */
    p.setAction = function(item,config){
        var action = config.action
        if( action != "none"){
            var hit = new createjs.Shape();
            hit.graphics.beginFill("#000").drawRect(0,0,item.getBounds().width,item.getBounds().height);  // opaque black 50x50 square
            item.hitArea = hit;
            item.on("click",this.onClickItem,this);
            switch (action.type){
                case "popup"://弹窗s
                    item.group_id = action.group_id;
                    break;
                case "jumpto"://跳转
                    item.page = action.page;
                    break;
                case "jumptoURL"://跳转外链
                    item.url = action.url;
                    break;
            }
            //window.location = "http://mywebsite.com/choose.php";

        }
    }

    /*
     * 点击事件
     * */
    p.onClickItem = function(evt){
        //console.log(evt.target)
        if(evt.target.page){
            contentLayer.jumpTo(evt.target.page);
        }
        if(evt.target.group_id){
            popupLayer.createPopup(evt.target.group_id);
        }
        if(evt.target.url){
            window.location =evt.target.url;
            location.reload();
        }
        this.setIconStatus(evt.target.name);
        //this.setIconStatus(evt.target);
    }

    /*
     * 设置animation
     * */
    p.setAnimation = function(item,config){
        var config = config.tween;
        //console.log(this.name);
        if(config){
            //console.log("aaaa")
            var tween = createjs.Tween.get(item,{loop:config.loop});
            if(config.wait){
                tween.wait(config.wait);
            }
            item.proObj = {};//记住对象的最终属性
            for(var key in config.prop){
                item.proObj[key] = item[key];//将目标参数记录在obj 中
                if(key!="y" && key!="x"){
                    item[key] = config.prop[key];//改变初始参数
                }else{
                    if(key=="y"){
                        item[key] = config.prop[key]*targetHeight;
                    }else{
                        item[key] = config.prop[key]*targetWidth;
                    }

                }

            }
            //console.log(item.proObj);
            tween.to(item.proObj, config.during,createjs.Ease[config.ease]);
            if(config.loop){
                tween.to(config.prop, config.during,createjs.Ease[config.ease]);
            }
            if(config.call){
                try
                {
                    tween.call(eval(config.call));
                }catch(e){
                    console.log(this.className+" no func '"+config.call+"'");
                    //alert("aa");

                }
            }

        }
    }

    /*
    * 自定义类类型
    * */
    p.drawClassType = function(config){
        //console.log(config.class_name)
        var classNameString = eval(config.class_name);
        //var item = new eosi.website.Music("sss")
        var item = new classNameString(config);
        item.name = config.id;
        this.addChild(item);

    }

    /*
    * 设置icons选中状态
    * */
    p.setIconStatus = function(sel_icon_name){
        //var curIcon = sel_icon;
        var curIcon = this.getChildByName(sel_icon_name);
        if(curIcon){
            for(var i = 0 ;i<this.children.length;i++){
                var item = this.getChildAt(i)
                if(curIcon == item){
                    if(!curIcon.isSelected){

                        this.setSelected(curIcon);
                    }
                    curIcon.isSelected = true;
                }else{
                    if(item.isSelected){
                        item.isSelected = false;
                        this.removeSelected(item);
                    }
                }
            }
        }
    }
    p.setSelected = function(item){
        var config = this.UIConfigs[this.menuGroupIndex].selected
        switch (config.type){
            case "tween":
                var tween = createjs.Tween.get(item,{loop:config.loop});
                if(config.wait){
                    tween.wait(config.wait);
                }
                item.proObj = {};//记住对象的最终属性
                var toObj={};
                for(var key in config.prop){
                    item.proObj[key] = item[key];//将目标参数记录在obj 中
                    if(key=="y"){
                        toObj[key] = item.proObj[key] + config.prop[key]*targetHeight;
                    }else if(key=="x"){
                        toObj[key] = item.proObj[key] + config.prop[key]*targetWidth;
                    }else if(key=="scaleX" || key=="scaleY"){
                        toObj[key] = item.proObj[key]+config.prop[key];
                    }else{
                        toObj[key] = config.prop[key];//改变初始参数
                    }

                }

                tween.to(toObj, config.during,createjs.Ease[config.ease]);
                if(config.loop){
                    tween.to(toObj, config.during,createjs.Ease[config.ease]);
                }
                if(config.call){
                    try
                    {
                        tween.call(eval(config.call));
                    }catch(e){
                        console.log(this.className+" no func '"+config.call+"'");
                        //alert("aa");

                    }
                }

                break;
        }
        //console.log(this.UIConfigs[this.menuGroupIndex].selected)
    }
    p.removeSelected = function(item){
        var config = this.UIConfigs[this.menuGroupIndex].selected
        switch (config.type){
            case "tween":
                var tween = createjs.Tween.get(item,{loop:config.loop});
                if(config.wait){
                    tween.wait(config.wait);
                }

                tween.to(item.proObj, config.during,createjs.Ease[config.ease]);
                if(config.loop){
                    tween.to(config.prop, config.during,createjs.Ease[config.ease]);
                }
                if(config.call){
                    try
                    {
                        tween.call(eval(config.call));
                    }catch(e){
                        console.log(this.className+" no func '"+config.call+"'");
                        //alert("aa");

                    }
                }

                break;
        }
    }


    p.tick = function(evt){

        for(var i=0;i<this.children.length;i++){
            if(this.getChildAt(i).tick){
                //console.log("hastick")
                this.getChildAt(i).tick();
            }
        }
    }

    // add MyApp to myNameSpace
    window.eosi.website.UILayer = createjs.promote(UILayer, "Container");
//}());
}(window));